package org.gienah.testing.xml;

import java.util.List;
import java.util.Map;

/**
 * @author Santiago L. Valdarrama
 * @since E0.2
 */
public class TestDefinition
		extends DependentDefinition {

	private boolean ignore;
	private boolean transactional;
	private final Map<String, MethodDefinition> methods;

	public TestDefinition(final Map<String, DependencyDefinition> dependencies,
			final List<String> contextFields,
			final boolean ignore,
			final boolean transactional,
			final Map<String, MethodDefinition> methods) {
		
		super(dependencies, contextFields);

		this.ignore = ignore;
		this.transactional = transactional;
		this.methods = methods;
	}

	public boolean isTransactionalMethod(final String method) {
		return this.transactional || isMethodDefinedAsTransactional(method);
	}

	public boolean isIgnoredMethod(final String method) {
		if (this.ignore) {
			return true;
		}

		if (this.methods != null) {
			MethodDefinition methodDefinition = this.methods.get(method);
			return methodDefinition != null
					? methodDefinition.isIgnore()
					: false;
		}

		return false;
	}

	private boolean isMethodDefinedAsTransactional(final String method) {
		if (this.methods != null) {
			MethodDefinition methodDefinition = this.methods.get(method);
			return methodDefinition != null
					? methodDefinition.isTransactional()
					: false;
		}

		return false;
	}
}
